export const lodash = {
	debounce: (func, wait = 500) => {
		let timeout;
		return function() {
			const context = this;
			const args = arguments;
			clearTimeout(timeout);
			timeout = setTimeout(() => {
				func.apply(context, args);
			}, wait);
		};
	},
	/**
	 * @param {*} fn 要执行的函数
	 * @param {*} interval 时间间隔
	 * @returns 
	 */
	throttle: (fn, interval = 500) => {
		let lastTime = 0
		const _throttle = function(...args) {
			const nowTime = new Date().getTime()
			const remainTime = interval - (nowTime - lastTime)
			if (remainTime <= 0) {
				// 2.3.真正触发函数
				fn.apply(this, args)
				// 2.4.保留上次触发的时间
				lastTime = nowTime
			}
		}
		return _throttle
	}
}
export const compareVersions = (currentVersion, latestVersion) => {
	const current = currentVersion.split('.').map(Number);
	const latest = latestVersion.split('.').map(Number);
	for (let i = 0; i < Math.max(current.length, latest.length); i++) {
		const a = current[i] || 0;
		const b = latest[i] || 0;
		if (a < b) return true;
		if (a > b) return false;
	}

	return false; // 版本相同
};
export const formatRichText = (html) => { // 富文本内容格式化
		/**
		 * @description 处理富文本内容，使图片自适应宽度并确保文本标签自动换行，各种形式的链接可点击跳转到WebView
		 * @param {string} html - 富文本HTML内容
		 * @returns {string} - 处理后的HTML内容
		 */
		if (!html) return
			//控制小程序中图片大小
			let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
				match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
				match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
				match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				return match;
			});
			newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
				match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
					'max-width:100%;');
				return match;
			});
			newContent = newContent.replace(/<br[^>]*\/>/gi, '');
			newContent = newContent.replace(/\<img/gi,
				'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
			newContent = newContent.replace(/\white-space:\s?pre\s?;/g,
				'white-space:pre-wrap;word-wrap: break-word;');
			return newContent;
	}